// 基础验证器函数
export const validators = {
  // 长度验证器 (6-20字符)
  lengthValidator: (value: string): { valid: boolean; message: string } => {
    if (value.length < 6 || value.length > 20) {
      return { valid: false, message: "输入长度必须在6-20个字符之间" };
    }
    return { valid: true, message: "" };
  },

  // 异步验证器示例 (模拟用户名唯一性检查)
  asyncValidator: async (
    value: string
  ): Promise<{ valid: boolean; message: string }> => {
    // 模拟API请求延迟
    await new Promise((resolve) => setTimeout(resolve, 800));

    // 模拟验证逻辑 - 假设"admin"已被占用
    if (value.toLowerCase() === "admin") {
      return { valid: false, message: "用户名已存在" };
    }

    return { valid: true, message: "" };
  },
  emailValidator: (value: string) => {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

    if (!emailRegex.test(value)) {
      return { valid: false, message: "请输入有效的邮箱地址" };
    }

    return { valid: true, message: "" };
  },
};
